<template>
    <div class="lb-customer-question-list">
        <top-nav></top-nav>
        <div class="page-main">

         <el-row class="page-top-operate">
            <lb-button type='primary' icon='el-icon-plus' @click="editQuestionnaire('add')">新增问卷</lb-button>
          </el-row>
          <el-table
                v-loading='loading'
                :data="tableData"
          :header-cell-style="{background:'#f5f7fa',color:'#606266'}"
                style="width: 100%">
                <el-table-column
                prop="id"
                label="ID">
                </el-table-column>
                <el-table-column
                prop="title"
                label="标题">
                </el-table-column>
                <el-table-column
                prop="pid"
                label="加入时间">
                    <template slot-scope="scope">
                      <p>{{scope.row.create_time | handleTime(1)}}</p>
                      <p>{{scope.row.create_time | handleTime(2)}}</p>
                    </template>
                </el-table-column>
                <el-table-column
                prop="address"
                label="是否开启">
                  <template slot-scope="scope">
                    <el-switch
                      v-model="scope.row.status"
                      :active-value="1"
                      :inactive-value="0"
                      @change="updateQuestionInfo(scope.row.id,scope.row.status)"
                      >
                    </el-switch>
                  </template>
                </el-table-column>
                <el-table-column
                prop="address"
                label="操作">
                    <template slot-scope="scope">
                        <div class="table-operate">
                            <lb-button size='mini' plain type='primary' @click="editQuestionnaire('edit',scope.row.id)">{{$t('action.edit')}}</lb-button>
                        </div>
                    </template>
                </el-table-column>
            </el-table>
            <lb-page
            :batch='false'
            :page='page'
            :pageSize='limit'
            :total='total'
            @handleSizeChange='handleSizeChange'
            @handleCurrentChange='handleCurrentChange'
            >
            </lb-page>
        </div>
    </div>
</template>

<script>
export default {
  data () {
    return {
      loading: false,
      page: 1,
      limit: 10,
      total: 0,
      tableData: []
    }
  },
  activated () {
    this.getTableDataList()
  },
  methods: {
    getTableDataList () {
      this.loading = true
      let {page, limit} = this
      this.$api.getCustomerQuestionsList({page, page_count: limit}).then(res => {
        this.loading = false
        if (res.code === 200) {
          this.tableData = res.data.data
          this.total = res.data.total
        }
      })
    },
    updateQuestionInfo (id, status) {
      this.$api.editCustomerQuestionInfo({data: {id, status}}).then(res => {
        if (res.code === 200) {
          this.$message.success(this.$t('tips.successOper'))
        }
        this.getTableDataList()
      })
    },
    handleSizeChange (val) {
      this.limit = val
      this.handleCurrentChange(1)
    },
    handleCurrentChange (val) {
      this.page = val
      this.getTableDataList()
    },
    editQuestionnaire (type, id) {
      let url = '/customer/qEdit'
      if (type === 'edit') {
        url += `?id=${id}`
      }
      this.$router.push(url)
    }
  },
  filters: {
    handleTime (val, type) {
      let time = type === 1 ? moment(val * 1000).format('YYYY-MM-DD') : type === 2 ? moment(val * 1000).format('HH:mm:ss') : moment(val * 1000).format('YYYY-MM-DD HH:mm:ss')
      return time
    }
  }
}
</script>

<style lang="scss" scoped>
    .lb-customer-question-list{
        width: 100%;
        .page-main{
            width: 100%;
        }
    }
</style>
